package org.stvd.repository.admin.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.stvd.entities.admin.Guids;
import org.stvd.repository.admin.GuidsDao;
import org.stvd.repository.base.impl.BaseDaoImpl;

@Repository("GuidsDao")
public class GuidsDaoImpl extends BaseDaoImpl<Guids> implements GuidsDao {

    public List<Guids> listAllGuids(String innerCode, String status) {
        String hql = "FROM Guids WHERE innerCode LIKE ?0 AND (status = ?1 OR '' = ?1)";
        return findByHQL(hql, innerCode+"%", status);
    }
    
    @Override
    public List<Guids> listAllChildGuids(String guid) {
        List<Guids> list = new ArrayList<>();
        Guids guids = this.findByPk(Guids.class, guid);
        if(guids != null) {
            list = findByHQL("FROM Guids WHERE innerCode LIKE ?0 ", guids.getInnerCode()+"%");
        }
        return list;
    }
    
    @Override
    public List<Guids> listGuidsByUpperGuid(String guid) {
        return findByHQL("FROM Guids WHERE upperGuid = ?0 ", guid);
    }
    
    @Override
    public List<Guids> listGuidByUserId(String userId) {
        String hql = "SELECT T1 FROM Guids T1, UserGuid T2 WHERE T1.guid = T2.guid AND T2.userId = ?0";
        return findByHQL(hql, userId);
    }
}
